/*
 * LoginController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.login.web, Version 1.0 (the "License");
 *
 */
package org.geek.app.login.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.geek.app.button.service.IButtonService;
import org.geek.app.module.service.IModuleService;
import org.geek.core.base.BaseController;
import org.geek.core.bean.ExtTree;
import org.geek.core.mybatis.domain.Button;
import org.geek.core.mybatis.domain.Criteria;
import org.geek.core.mybatis.domain.User;
import org.geek.core.util.TreeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
@Controller
public class LoginController extends BaseController{
	
	@Resource
	private IModuleService moduleService;
	@Resource
	private IButtonService buttonService;

	@RequestMapping(value="index.jhtml",method = RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request, ModelMap model) {
		User user = this.getUser(request);
		List<ExtTree> list = null;
		if(isAdmin(user)){
			list = moduleService.getModules2Tree();
		}else{
			list = moduleService.getModuleByAuth(user.getUserId());
		}
		model.put("nav", TreeUtil.getTree(list, 1));
//		model.put("nav",TreeUtil.getDesktopTree(list,1) );
		return "index";
	}
	
	@RequestMapping(value="loadActions.jhtml",method = RequestMethod.POST)
	@ResponseBody
	public Object loadActions(int moduleId,String owerType,HttpServletRequest request){
		User user = this.getUser(request);
		List<Button> buttons = new ArrayList<Button>();
		if(isAdmin(user)){
			Criteria criteria = new Criteria();
			criteria.put("moduleId", moduleId);
			buttons = buttonService.getButtonByCriteria(criteria);
		}else{
			buttons = buttonService.getButtonByAuth(moduleId,user.getUserId());
		}
		 List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		 for(Button btn:buttons){
			 Map<String,Object> map = new HashMap<String, Object>();
				map.put("ename", btn.getButtonId());
				map.put("name", btn.getButtonName());
				map.put("icon", btn.getIconCls());
				map.put("handler", btn.getHandler());
				map.put("tooltip", btn.getTooltip());
				list.add(map); 
		 }
		return list;
	}
}